package com.ruoyi.project.rate.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.rate.Utils;
import com.ruoyi.project.rate.domain.KhzDept;
import com.ruoyi.project.rate.service.KhzDeptService;

@Controller
@RequestMapping("/rate/khzDept")
public class KhzDeptController extends BaseController {
	@Autowired
	private KhzDeptService khzDeptService;

	@RequestMapping("/khzDept")
	public String cp(KhzDept khzDept, ModelMap mmap) {
		mmap.put("khzDept", khzDept);

		return "rate/khzDeptList";
	}

	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(KhzDept khzDept) {
		List<KhzDept> list = this.khzDeptService.findList(khzDept);
		return getDataTable(list);
	}

	@RequestMapping("/add")
	public String add(KhzDept khzDept, ModelMap mmap) {
		khzDept.setManager("N");
		mmap.put("khzDept", khzDept);
		mmap.put("deptList", Utils.level2DeptList());

		return "rate/khzDeptForm";
	}

	@RequestMapping("/edit")
	public String edit(KhzDept khzDept, ModelMap mmap) {
		String pageFlag = khzDept.getPageFlag();
		khzDept = this.khzDeptService.get(khzDept);
		if (khzDept == null) {
			throw new RuntimeException("找不到对应的数据！");
		}

		khzDept.setPageFlag(pageFlag);
		mmap.put("khzDept", khzDept);

		return "rate/khzDeptForm";
	}

	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(KhzDept khzDept) {
		this.khzDeptService.save(khzDept);

		return AjaxResult.success("保存成功！");
	}

	@RequestMapping("/delete/{id}")
	@ResponseBody
	public AjaxResult delete(@PathVariable("id") String id) {
		if (StringUtils.isBlank(id)) {
			throw new RuntimeException("请求参数错误：【id=" + id + "】");
		}

		KhzDept khzDept = this.khzDeptService.get(id);
		if (khzDept == null) {
			throw new RuntimeException("找不到对应的数据！");
		}

		this.khzDeptService.delete(khzDept);

		return AjaxResult.success("删除成功！");
	}
}